<?php
// This file contains all User related functions.
require_once (ROOT . BRANCH . '/library/db.php');

function redirect($page) {
	// Redirects the browser to the given page
	// MUST be called before outputting any HTML to the browser
	if (headers_sent($file, $line)) {
		print "Headers already sent: $file, $line";
		return;
	}
	$host = HOST;
	$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
	header("Location: http://$host$uri/$page");
	exit;
}

function getParams($url) {
	if($url) {
		$array = explode('&',$url);
		foreach($array as $a) {
			$p = explode('=',$a);
			$params->$p[0] = $p[1];
		}
		return $params;
	} else return false;
}

function isLoggedIn() {
	if (isset($_SESSION['logged_in'])) return true;
	else return false;
}

function logout() {
	unset($_SESSION['instructor']);
	session_unset();
	session_destroy();
	redirect('index.php?m=1');
}

function printMsg($msg) {
	if(isLoggedIn()) $bubble = "<div class='bubble-top'></div>";
	else $bubble = '';
	print "<div id='update-msg'>
			$bubble
			<p>$msg</p>
			</div>";
}

function makePassword($password) {
	return md5($password.salt);
}

function generateId($name) {
	if(strpos($name,'.php'))
		$name = substr($name,0,strpos($name,'.php'));
	$n = strtolower($name);
	if(strpos($name,'_'))
		$array = explode('_',$n);
	else
		$array = explode(' ',$n);
	$id = $array[0];
	array_shift($array);
	foreach($array as $a) {
		$id .= "-$a";
	}
	return $id;
}